﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_RegionAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataToCountryDropdown();
            BindDataWhenEdit();
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblMyCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblRegionName.Text = MultipleLanguage.LBL_REGIONNAME[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
    }

    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataWhenEdit();
    }
    #endregion

    #region Relative Functions
    private void ValidateControl()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        rfvRegionname.ErrorMessage = MessagesValidate.REGION_NAME_EMPTY[nDefaultLanguage - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);

                if (nID > 0)
                {
                    Region objRegion = new Region();
                    objRegion.LoadById(nID);
                    ddlMyCountry.SelectedValue = ConvertData.ConvertToString(objRegion.Data.CountryID);
                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtRegionName.Text = ConvertData.ConvertToString(objRegion.Data.RegionName1);
                            break;
                        case 2:
                            txtRegionName.Text = ConvertData.ConvertToString(objRegion.Data.RegionName2);
                            break;
                        case 3:
                            txtRegionName.Text = ConvertData.ConvertToString(objRegion.Data.RegionName3);
                            break;
                    }

                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_REGION;
            Response.Redirect(sLink);
        }
    }
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlMyCountry.DataSource = dtbCountry;
        ddlMyCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlMyCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlMyCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlMyCountry.DataTextField = "CountryName3";
                break;
        }
        ddlMyCountry.DataBind();
    }
    private eRegion SetData()
    {
        Region objRegion = new Region();
        eRegion eRegion = this.RegionEntity();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        objRegion.Data.CountryID = ConvertData.ConvertToInt(ddlMyCountry.SelectedValue);
        switch (nCurrentLanguage)
        {
            case 1:
                objRegion.Data.RegionName1 = ConvertData.ConvertToString(txtRegionName.Text.Trim());

                objRegion.Data.RegionName2 = ConvertData.ConvertToString(eRegion.RegionName2);
                objRegion.Data.RegionName3 = ConvertData.ConvertToString(eRegion.RegionName3);
                break;
            case 2:
                objRegion.Data.RegionName2 = ConvertData.ConvertToString(txtRegionName.Text.Trim());

                objRegion.Data.RegionName1 = ConvertData.ConvertToString(eRegion.RegionName1);
                objRegion.Data.RegionName3 = ConvertData.ConvertToString(eRegion.RegionName3);
                break;
            case 3:
                objRegion.Data.RegionName3 = ConvertData.ConvertToString(txtRegionName.Text.Trim());

                objRegion.Data.RegionName1 = ConvertData.ConvertToString(eRegion.RegionName1);
                objRegion.Data.RegionName2 = ConvertData.ConvertToString(eRegion.RegionName2);
                break;
        }
        return objRegion.Data;
    }
    private eRegion RegionEntity()
    {
        eRegion entityRegion = new eRegion();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Region objRegion = new Region();
            objRegion.LoadById(nItemID);
            entityRegion = objRegion.Data;
        }
        return entityRegion;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        Region objRegion = new Region();

        objRegion.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objRegion.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_REGION + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objRegion.Update(nID);
                if (nResult > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_REGION);

    }
    #endregion
}